import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JPanel;


public class BoutonsFormes extends JPanel {
	private static final long serialVersionUID = 1L;

	private CreateurDessin createurCourant;
	private PropertyChangeSupport changes;
	
	public BoutonsFormes()
	{
		changes = new PropertyChangeSupport(this);
		//setLayout(new GridLayout(4,1));
		GridBagLayout g = new GridBagLayout();
		setLayout(g);
		GridBagConstraints c = new GridBagConstraints () ;
		c.fill = GridBagConstraints.BOTH ; 
		c.weightx = 0 ;
		c.gridwidth = GridBagConstraints.REMAINDER ;
		JButton temp = new JButton(new ImageIcon("images/iconRectangle.png"));
		temp.addActionListener(new ButtonListener(new CreateurRectangle()));
		g.setConstraints(temp,c);
		add(temp);

		temp = new JButton(new ImageIcon("images/iconTriangleIso.png"));
		temp.addActionListener(new ButtonListener(new CreateurTriangleV()));
		g.setConstraints(temp,c);
		add(temp);
		
		temp = new JButton(new ImageIcon("images/iconTriangleH.png"));
		temp.addActionListener(new ButtonListener(new CreateurTriangleH()));
		g.setConstraints(temp,c);
		add(temp);

		temp = new JButton(new ImageIcon("images/iconTriangleRect.png"));
		temp.addActionListener(new ButtonListener(new CreateurTriangleHG()));
		g.setConstraints(temp,c);
		add(temp);

		temp = new JButton(new ImageIcon("images/iconEllipse.png"));
		temp.addActionListener(new ButtonListener(new CreateurEllipse()));
		g.setConstraints(temp,c);
		add(temp);
		temp = new JButton(new ImageIcon("images/iconHexagone.png"));
		temp.addActionListener(new ButtonListener(new CreateurHexagone()));
		g.setConstraints(temp,c);
		add(temp);
	}
	
	/* Accessors */
	public void setCurrentButton(CreateurDessin c)
	{
		CreateurDessin oldCreateur = createurCourant;
		createurCourant = c;
		changes.firePropertyChange("Forme", oldCreateur, createurCourant);
	}
	
	public void addPropertyChangeListener(PropertyChangeListener l)
	{
		changes.addPropertyChangeListener(l);
	}
	
	public void removePropertyChangeListener(PropertyChangeListener l)
	{
		changes.removePropertyChangeListener(l);
	}
	
	/* Action Listeners Methods */
	class ButtonListener implements ActionListener {

		CreateurDessin createur;
		
		public ButtonListener(CreateurDessin c)
		{
			createur = c;
		}
		
		@Override
		public void actionPerformed(ActionEvent e) {
			BoutonsFormes.this.setCurrentButton(createur);
		}
		
	}
}
